package com.invexer.common.controller.init {
import com.invexer.common.constant.ContextCommandConst;
import com.invexer.common.puremvc.patterns.command.InvexerSimpleCommandBase;
import com.invexer.service.baosteel.BaoSteelHttpService;
import com.invexer.service.baosteel.BaoSteelWebService;
import com.invexer.service.linggang.LingGangHttpService;

import flash.events.Event;

import org.puremvc.as3.multicore.interfaces.INotification;

public class ContextStartupBootstrapBaseCMD extends InvexerSimpleCommandBase {
	protected var _context:Object;
	protected var _appConfigPath:String;
	protected var _appConfig:Object;
	private var _linggangHttpService:LingGangHttpService;
	private var _baoSteelWebService:BaoSteelWebService;
	private var _baoSteelHttpService:BaoSteelHttpService;

	override public function execute(notification:INotification):void{
		super.execute(notification);

		var body:Object = notification.getBody();
		_context = body.context;
		_appConfigPath = body.appConfigPath;

		initRozennLog();
	}

	protected function initRozennLog():void{
		facade.registerCommand(ContextCommandConst.ContextInitLogCMD, ContextInitLogCMD);
		sendNotification( ContextCommandConst.ContextInitLogCMD, {
			context:_context,
			callback:onInitRozennLogComplete
		});
		facade.removeCommand(ContextCommandConst.ContextInitLogCMD);
	}

	protected function onInitRozennLogComplete():void{
		loadContextConfig();
	}

	protected function loadContextConfig():void{
		facade.registerCommand(ContextCommandConst.ContextInitConfigCMD, ContextInitConfigCMD);
		sendNotification( ContextCommandConst.ContextInitConfigCMD, {
			appConfigPath:_appConfigPath,
			callback:onLoadContextConfigComplete
		});
		facade.removeCommand(ContextCommandConst.ContextInitConfigCMD);
	}

	protected function onLoadContextConfigComplete(config:Object):void{
		_appConfig = config;

		_context.styleManager.loadStyleDeclarations(config.theme);

		_context.addEventListener(Event.REMOVED_FROM_STAGE, contextOnRemoveFromStage)

		initFramework();
	}

	protected function initFramework():void{
//		facade.registerCommand(ContextCommandConst.PriceInitFrameworkCMD, PriceInitFrameworkCMD);
//		sendNotification( ContextCommandConst.PriceInitFrameworkCMD, {
//			context:_context,
//			appConfig:_appConfig
//		} );
//		facade.removeCommand(ContextCommandConst.PriceInitFrameworkCMD);
	}


	//event handle
	protected function contextOnRemoveFromStage(event:Event):void {
		_context = null;
		_appConfig = null;
		_appConfigPath = null;
		
		event.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, contextOnRemoveFromStage);
//
//		facade.registerCommand(ContextCommandConst.PriceFinalizeFrameworkCMD, PriceFinalizeFrameworkCMD);
//		sendNotification( ContextCommandConst.PriceFinalizeFrameworkCMD);
//		facade.removeCommand(ContextCommandConst.PriceFinalizeFrameworkCMD);

	
	}

}
}
